package com.taikang.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by zhai on 16/5/16.
 */
public class Common {

    public static String randNumber(int length) {
        String[] baseStr = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
        StringBuilder br = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int index = ((int) (100 * Math.random())) % baseStr.length;
            br.append(baseStr[index]);
        }
        return br.toString();
    }

    public static final String TIMEF_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_HOUR_FORMAT = "yyyy-MM-dd HH:mm";

    public static String dateToDateString(Date date) {
        return dateToDateString(date, TIMEF_FORMAT);
    }

    public static String dateToDateString(Date date, String formatStr) {
        DateFormat df = getDateFormat(formatStr);
        return df.format(date);
    }

    private static DateFormat getDateFormat(String formatStr) {
        return SimpleDateFormatFactory.getInstance(TIMEF_FORMAT);
    }

    public static Date convertStringToDateTime(String date) throws ParseException {
        return SimpleDateFormatFactory.getInstance(DATE_HOUR_FORMAT).parse(date);
    }
}
